package com.flyme.module.base.provider.api;

import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.common.core.security.OpenHelper;
import com.flyme.common.core.security.OpenUserAuth;
import com.flyme.common.core.utils.ObjectUtils;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.entity.SysUser;
import com.flyme.module.base.provider.service.*;
import com.flyme.module.file.client.entity.SysCourse;
import com.flyme.module.file.provider.service.*;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @author flyme
 */
@Slf4j
@Api(tags = "app课程管理")
@RestController
@RequestMapping("/course")
public class ApiCourseController extends BaseController<SysCourseService, SysCourse> {


    @Autowired
    private SysInvoiceService invoiceService;
    @Autowired
    private SysOffCourseService offCourseService;
    @Autowired
    private SysLecturerService lecturerService;
    @Autowired
    private SysUserService userService;
    @Autowired
    private SysCourseEnrollService courseEnrollService;
    @Autowired
    private SysCourseTypeService courseTypeService;
    @Autowired
    private SysLiveService liveService;
    @Autowired
    private SysHistoryService historyService;
    @Autowired
    private SysUsercertificateService sysUsercertificateService;


    /**
     * 小程序最近在学/购买课程页面
     */
    @PostMapping(value = "/studyPlan")
    public ResultBody studyPlan(PagerInfo pagerInfo, Integer selectType) {
        OpenUserAuth openUserAuth = OpenHelper.getUserAuth();
        PqGrid list = new PqGrid();
        if(selectType.equals(1)){//最近在学
            list = historyService.selectByUserWeb2(openUserAuth.getUserId(), pagerInfo);
        }else{
            list = courseEnrollService.findByUser(openUserAuth.getUserId(), pagerInfo);
        }
        return ResultBody.success(list);
    }

    /**
     *  小程序查看证书
     */
    @PostMapping("/showCertifite")
    public ResultBody downCertifite(Long userCertifiId) {
        if(ObjectUtils.isEmpty(userCertifiId)){
            return ResultBody.failed("参数不能为空");
        }
        PagerInfo info=new PagerInfo();
        CriteriaQuery<EntityMap> qw = new CriteriaQuery(info);
        qw.select("certificate.certificateName,u.validity,u.awardTime,u.certificateNum,u.userId,u.idCard");
        qw.eq("u.id", userCertifiId);
        qw.createAlias("certificate", "certificate");
        List<EntityMap> userctList = sysUsercertificateService.pageList(qw).getRows();
        EntityMap usercertifite = userctList.get(0);
        SysUser user = null;
        if(ObjectUtils.isNotEmpty(usercertifite.get("idCard"))){
            user = userService.findByIdCard(usercertifite.get("idCard").toString());
        }
        if(ObjectUtils.isNotEmpty(usercertifite.get("userId"))){
            user = userService.getById(usercertifite.get("userId"));
        }
        if(ObjectUtils.isNotEmpty(user)){
            usercertifite.put("userName", user.getUserName());
            usercertifite.put("idCard", user.getIdCard());
            usercertifite.put("avatar", user.getAvatar());
            usercertifite.put("positionName", usercertifite.get("certificateName").toString());
        }
        return ResultBody.success(usercertifite);
    }


}
